class Proto {

	private protoPool: any = {}
	private msgClassDict = {};

	public constructor() {

	}

	//加载协议
	public load() {
		var files: Array<RES.ResourceItem> = RES.getGroupByName('protobuf');
		for (var k in files) {
			var file = files[k];
			console.log('解释协议文件', file.name);
			var data = RES.getRes(file.name);
			var proto = dcodeIO.ProtoBuf.loadProto(data);
			this.protoPool[file.name] = proto;
		}
	}
	public msgclass(msgName: string): any {
		var cls = this.msgClassDict[msgName];
		if (!cls) {
			for (var k in this.protoPool) {
				var proto = this.protoPool[k];
				cls = proto.build(msgName);
				if (cls) {
					this.msgClassDict[msgName] = cls;
					break;
				}
			}
		}
		if (!cls) {
			console.log('proto not found', msgName);
			return;
		}
		return cls;
	}

	public msgnew(msgName: string): any {
		var cls = this.msgclass(msgName);
		var msg = new cls;
		msg.__fullname = msgName;
		return msg;
	}


	public getClsByCmd(_cmd: number): any {
		var cmdcls:any=null;
		var cls:any=null;
		var msgName:string="";
		for (var k in this.protoPool) {
			var proto = this.protoPool[k];
			var protoname = k.split('_')[0];
			cmdcls = proto.build(protoname+".Cmd");
			if(cmdcls !=null){
				for(var key in cmdcls){
					if(cmdcls[key] == _cmd){
						var arr = key.split('CMD_');
						msgName = arr[1];
						cls = proto.build(protoname+"."+msgName);
						this.msgClassDict[msgName] = cls;
						break;
					}
				}
			}
		}
		return cls;
	}


}
